package com.house.web.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.house.biz.DistrictBiz;
import com.house.biz.HouseBiz;
import com.house.biz.StreetBiz;
import com.house.biz.TypeBiz;
import com.house.biz.UserBiz;
import com.house.entity.District;
import com.house.entity.House;
import com.house.entity.Street;
import com.house.entity.Type;
import com.house.entity.User;

@Controller
@RequestMapping("/admin")
public class AdminDefault {
	
	@Autowired
	private HouseBiz houseBiz;
	@Autowired
	private TypeBiz typeBiz;
	@Autowired
	private DistrictBiz districtBiz;
	@Autowired
	private StreetBiz streetBiz;
	@Autowired
	private UserBiz userBiz;
	
	@RequestMapping("admin_default")
	public String admin_default(){
		return "admin_default";
	}
	
	@RequestMapping("admin_house")
	public String admin_house(Model model){
		List<House> houses =houseBiz.getAllHouses();
		model.addAttribute("houses", houses);
		return "admin_house";
	}
	
	@RequestMapping("houseDelete")
	public String houseDelete(int id){
		houseBiz.deleteHouse(id);
		return "admin_house";
	}
	
	@RequestMapping("/admin_type")
	public String admin_type(Model model){
		List<Type> types =typeBiz.getAllType();
		model.addAttribute("types", types);
		return "admin_type";
	}
	
	@RequestMapping("/typeUpdate")
	public String typeUpdate(int id,Model model){
		Type type =typeBiz.getTypeById(id);
		model.addAttribute("type", type);
		return "admin_typeUpdate";
	}
	
	@RequestMapping("/typeAmend")
	public String typeAmend(Type type){
		if(type.getId() != 0){
			typeBiz.updateType(type);
		}else {
			typeBiz.addType(type);
		}
		return "redirect:admin_type";
	}
	
	@RequestMapping("/typeDelete")
	public String typeDelete(int id){
		typeBiz.delType(id);
		return "redirect:admin_type";
	}
	
	@RequestMapping("/admin_Street")
	public String admin_Street(Model model){
		List<Street> streets =streetBiz.getAllStreets();
		model.addAttribute("streets", streets);
		return "admin_Street";
	}
	
	@RequestMapping("/streetDelete")
	public String streetDelete(int id){
		streetBiz.delStreet(id);
		return "redirect:admin_Street";
	}
	@RequestMapping("/admin_StreetUpdate")
	public String admin_StreetUpdate(@RequestParam(defaultValue = "0", required = false)int id, Model model){
		if (id!=0) {
			Street street =streetBiz.getStreetById(id);
			model.addAttribute("street", street);
			
		}
		List<District> districts =districtBiz.getAllDistricts();
		model.addAttribute("districts", districts);
		return "admin_StreetUpdate";
	}
	
	@RequestMapping("/streetAmend")
	public String streetAmend(Street street){
		if (street.getId()>0) {
			streetBiz.updateStreet(street);
		}else {
			streetBiz.addStreet(street);
		}

		return "redirect:admin_Street";
	}
	
	@RequestMapping("/admin_District")
	public String admin_District(Model model){
		List<District> districts =districtBiz.getAllDistricts();
		model.addAttribute("districts", districts);
		return "admin_District";
	}
	
	@RequestMapping("/admin_DistrictUpdate")
	public String admin_DistrictUpdate(@RequestParam(defaultValue = "0", required = false)int id, Model model){
		if (id!=0) {
			District district =districtBiz.getByDistrictId(id);
			model.addAttribute("district", district);
			
		}
		return "admin_DistrictUpdate";
	}
	
	@RequestMapping("/districtAmend")
	public String districtAmend(District district){
		if (district.getId()>0) {
			districtBiz.updateDistrict(district);
		}else {
			districtBiz.addDistrict(district);
		}

		return "redirect:admin_District";
	}
	
	@RequestMapping("/districtDelete")
	public String districtDelete(int id){
		districtBiz.delDistrict(id);
		return "redirect:admin_District";
	}
	
	@RequestMapping("/admin_User")
	public String admin_User(Model model){
		List<User> users =userBiz.getAllUsers();
		model.addAttribute("users", users);
		return "admin_User";
	}
	
	@RequestMapping("/userDelete")
	public String userDelete(int id){
		userBiz.delUser(id);
		return "redirect:admin_User";
	}
	
}
